#include "cmenubutton.h"

Cmenubutton::Cmenubutton(QWidget * parent) : QPushButton(parent)
{
    status = NORMAL;
    mouse_press = false;
    icon_path = "/new/icon/";

    Cmenubutton::setFixedSize(44,44);
    Cmenubutton::setStyleSheet("border:none");

    buttonImage = new QLabel();
    buttonImage->setFixedSize(30,30);
    buttonImage->setStyleSheet("QLabel{border-image:url(:" + normalImg + ")}");

    buttonTxt = new QLabel();
    buttonTxt->setFixedHeight(12);
    buttonTxt->setStyleSheet("color:white");
    QFont f;
    f.setPointSize(6);
    buttonTxt->setFont(f);
    buttonTxt->setAlignment(Qt::AlignHCenter|Qt::AlignTop);

    QVBoxLayout *topLayout = new QVBoxLayout();
    topLayout->addWidget(buttonImage);
    topLayout->setContentsMargins(7,0,0,0);

    QVBoxLayout *bottomLayout = new QVBoxLayout();
    bottomLayout->addWidget(buttonTxt);
    bottomLayout->setMargin(0);
    bottomLayout->setSpacing(0);

    QVBoxLayout *mainLayout = new QVBoxLayout();
    mainLayout->setMargin(0);
    mainLayout->setSpacing(0);
    mainLayout->addLayout(topLayout);
    mainLayout->addLayout(bottomLayout);

    Cmenubutton::setLayout(mainLayout);
}

void Cmenubutton::setNormalImg(QString s) {
    normalImg = s;
    buttonImage->setStyleSheet("QLabel{border-image:url(:" + normalImg + ")}");
}

void Cmenubutton::sethoverImg(QString s) {
    hoverImg = s;
}

void Cmenubutton::setPressImg(QString s) {
    pressImg = s;
}

void Cmenubutton::setButtonTxt(QString s) {
    buttonTxt->setText(s);
}

void Cmenubutton::enterEvent(QEvent *)
{
    status = HOVER;
    buttonImage->setStyleSheet("QLabel{border-image:url(:" + hoverImg + ")}");
    buttonTxt->setStyleSheet("color:#919191");
    update();
}

void Cmenubutton::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        mouse_press = true;
        status = PRESS;
    }
}

void Cmenubutton::mouseReleaseEvent(QMouseEvent *event)
{
    if(mouse_press&&this->rect().contains(event->pos()))
    {
        mouse_press = false;
        status = HOVER;
        emit clicked();
    }
}

void Cmenubutton::leaveEvent(QEvent *)
{
    status = NORMAL;
    buttonImage->setStyleSheet("QLabel{border-image:url(:" + normalImg + ")}");
    buttonTxt->setStyleSheet("color:white");
}

